using MediatR;
using UniversalAdmin.Application.Common;
using UniversalAdmin.Application.DTOs;
using UniversalAdmin.Application.Services;   // MenuTreeBuilder

namespace UniversalAdmin.Application.Features.Menus.Queries.GetMenuTree;

public class GetMenuTreeQueryHandler : IRequestHandler<GetMenuTreeQuery, ApiResult<List<MenuTreeDto>>>
{
  private readonly MenuTreeBuilder _builder;

  public GetMenuTreeQueryHandler(MenuTreeBuilder builder)
  {
    _builder = builder;
  }

  public async Task<ApiResult<List<MenuTreeDto>>> Handle(GetMenuTreeQuery request, CancellationToken ct)
  {
    var tree = await _builder.BuildMenuTreeAsync();
    return ApiResult<List<MenuTreeDto>>.Success(tree);
  }
}